import { Component, ElementRef, OnInit, Input, ViewChild, Output, EventEmitter, AfterViewInit } from '@angular/core';

import { Service } from '../../server/server';
import { TaskTableComponent } from '../task/task-table/task-table.component';
import { Pagination } from '../../public/serve/pcrudInfo';
import { RemoveTableService } from '../../public/serve/remove-table/remove-table.service';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';

declare var $:any;

interface Response {
  result: string[];
  result_code: string;
  message: string;
  length: number;
}

@Component({
  selector: 'app-task',
  templateUrl: './task.component.html',
})


export class TaskComponent implements OnInit {
  
  tableNumber : number = 0;
  right: any = { "system" : {}, "task" : {} };
  constructor(private server:Service,private removeTableService: RemoveTableService,private http: HttpClient) {
    this.right = $.extend(this.right, server.getRight());
  }
  
  public isActive= this.server.isActive;
  ngOnInit() {
    this.server.eventEmit.subscribe((value: any) => {
      this.isActive=value;
		});
  }

  ngDoCheck(){
    this.right = $.extend(this.right, this.server.getRight());
  }

  //导入table
	@ViewChild("table")
	table: TaskTableComponent;

	// 删除按钮
  isSelect : boolean = true;
  isSelectFromTableHandler(isSelect: boolean) { 
    this.isSelect = isSelect;
  }

  // 禁用
  reduction() {
    setTimeout(()=>{
      this.isSelect = true;
    },100);
  }

	// 表格
	public totalItems: number;
	ParentList(totalItems: number) {
		this.totalItems = totalItems;
	}
	
	//新增
  public newly_added_result_code: string;
  ParentAdd(result_code: string) {
    this.table.getTabList(this.pageNumber, this.pageSize, '0',1,this.taskName);
	}
	
	// 修改
  public edit_result_code : string;
  ParentUpdate(result_code: string) {
    this.table.getTabList(this.pageNumber, this.pageSize, '0',1,this.taskName);
	}

	//删除
  public remove_result_code: string;
  ParentDelete(result_code: string) {
    this.table.getTabList(this.pageNumber, this.pageSize, '0',1,this.taskName);
  }
	
	// 当前页（默认为第一页）
	public pageNumber: number = 1;
	// 每页显示多少条数据（默认每页显示20条）
	public pageSize: number = 20;
	// id
	public id: string = '0';
	// 分页
	public pagination: Pagination;
	isLoadTableFromPaginationHandler(pagination: Pagination) {
		this.pagination = pagination;

		this.pageNumber = this.pagination[0].pageNumber;
		this.pageSize = this.pagination[0].pageSize;
		this.id = this.pagination[0].id;

		this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

		this.table.getTabList(this.pageNumber, this.pageSize, '0',1,this.taskName);
  }
  
  //查询
  public taskName:any = '';
  ParentSearch(val){
    //console.log(val);
    this.taskName = val;
    this.table.getTabList(this.pageNumber, this.pageSize, '0',1,this.taskName);
    //this.server.tasksearcheventEmit.emit(this.taskName);
  }

	//开启/关闭服务
	opencloseservice(val){
		//console.log(this.removeTableService.getRemoveTableId)
    let url = this.server.baseUrl + 'admin/tasks/updateStatus';
    let data = {
      taskId:this.removeTableService.getRemoveTableId,
      taskStatus:val
		}
		//console.log(data)
    this.http.post<Response>(url, data,{}).subscribe(res => {
			//console.log(res)
			this.table.cancelSelectAll();
      if (res.result_code == '0000') {
        this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: res.message }
        this.server.promptclose;     
        this.removeTableService.getRemoveTableId=[];
				this.table.getTabList(this.pageNumber, this.pageSize, '0',1,this.taskName); 	
      } else {
        this.removeTableService.getRemoveTableId=[]
        this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: res.message }
				this.server.promptclose;
				this.table.getTabList(this.pageNumber, this.pageSize, '0',1,this.taskName);
      }
    });
	}

}
